package com.lyn.watches;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.text.SimpleDateFormat;
import java.util.Date;

public class WatchesView3 extends View {
    private Paint paint;
    private Context context;
    private float percentage = 0.6f;
    private Bitmap heartBitmap;
    private SimpleDateFormat hmSimpleDateFormat;
    private SimpleDateFormat msSimpleDateFormat;
    private String modeLabel="陶老板模式";

    public WatchesView3(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public WatchesView3(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    /**
     * 设置模式标签显示
     * @param modeLabel
     */
    public void setModeLabel(String modeLabel) {
        this.modeLabel = modeLabel;
        postInvalidate();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(20f);
        paint.setTextAlign(Paint.Align.CENTER);
        hmSimpleDateFormat=new SimpleDateFormat("HH:mm");
        msSimpleDateFormat=new SimpleDateFormat("mm:ss");

        heartBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heart3, null);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        invalidate();
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        int centerX = width / 2;
        int centerY = height / 2;

        paint.setColor(context.getResources().getColor(R.color.black));
        canvas.drawRoundRect(0,0,width,height,15f,15f,paint);
        int padding = 20;
        RectF rectF = new RectF();
        rectF.left = 0 + padding;
        rectF.top = 0 + padding;
        rectF.right = width - padding;
        rectF.bottom = height - padding;
        int r = (int) ((rectF.right - rectF.left) / 2);

        int progressRate = (int) (360 * percentage);
        //线条粗细
        int lineThickness=20;
        paint.setStrokeWidth(lineThickness);
        paint.setStyle(Paint.Style.STROKE);

        paint.setColor(context.getResources().getColor(R.color.colorPinkBlack));
        canvas.drawArc(rectF, 0, 360, false, paint);

        paint.setColor(context.getResources().getColor(R.color.colorPink));
        canvas.drawArc(rectF, 270, progressRate, false, paint);

        int startX = (int) (r * Math.cos(270 * Math.PI / 180) + centerX);
        int startY = (int) (r * Math.sin(270 * Math.PI / 180) + centerY);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(startX, startY, lineThickness/2, paint);

        int endX = (int) (r * Math.cos((270 + progressRate - 360) * Math.PI / 180) + centerX);
        int endY = (int) (r * Math.sin((270 + progressRate - 360) * Math.PI / 180) + centerY);
        canvas.drawCircle(endX, endY, lineThickness/2, paint);

        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
        float indentationWidth = lineThickness*2;
        float indentationHeight = lineThickness*2;
        RectF contentRectF = new RectF();
        contentRectF.left = rectF.left + indentationWidth;
        contentRectF.top = rectF.top + indentationHeight;
        contentRectF.right = rectF.right - indentationWidth;
        contentRectF.bottom = rectF.bottom - indentationHeight;

//        canvas.drawRoundRect(contentRectF, 10, 10, paint);

        float heartSize = lineThickness;
        RectF heartRectF = new RectF();
        heartRectF.left = contentRectF.left ;
        heartRectF.top = contentRectF.top ;
        heartRectF.right = heartRectF.left + heartSize;
        heartRectF.bottom = heartRectF.top + heartSize;
        canvas.drawBitmap(heartBitmap, null, heartRectF, paint);

        paint.setStyle(Paint.Style.FILL);
        paint.setColor(context.getResources().getColor(R.color.white));


        String hmFormat = hmSimpleDateFormat.format(new Date(System.currentTimeMillis()));
        canvas.drawText(hmFormat,centerX
                ,baseLine((heartRectF.bottom-heartRectF.top)/2+heartRectF.top),paint);

        String msFormat = msSimpleDateFormat.format(new Date(System.currentTimeMillis()));
        canvas.drawText(msFormat,centerX,baseLine(centerY),paint);

        canvas.drawText(modeLabel,centerX
                ,baseLine(contentRectF.bottom-lineThickness/2),paint);
    }

    /**
     * 设置比例
     *
     * @param progress
     */
    public void setProgress(float progress) {
        percentage = 0;
        invalidate();
        if (progress > 1) {
            progress = 1f;
        }
        if (progress < 0) {
            progress = 0;
        }
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(percentage, progress);
        valueAnimator.setDuration(800);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                percentage = (float) valueAnimator.getAnimatedValue();
                invalidate();
            }
        });
        valueAnimator.start();

    }

    private float baseLine(float y) {
        //计算baseline
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        float baseline = y + distance;
        return baseline;
    }
}
